/**
 * 
 */
package com.nextive.interview.util;

import static com.nextive.interview.ApplicationManager.getAppContext;
import static com.nextive.interview.activity.view.DefaultTextValuesHolder.fetchValueFor;

import java.util.Calendar;
import java.util.regex.Pattern;

import android.view.View;
import android.widget.Toast;

/**
 * This class contains several method needed when dealing with different type of validations.
 * @author Cesar Roman
 *
 */
public class ValidationUtil {
	
	/**
	 * Email pattern for validating.
	 */
	private static final String emailPattern = ".+@.+\\.[a-z]+";
	
	/**
	 * It checks if a given input string is valid.
	 * @param input string to validate
	 * @return true if is valid
	 */
	public static boolean isValidStringInput(String input) {
		
		return input != null && !"".equals(input);
		
	}
	
	/**
	 * It checks if a given email string is valid matching it with a specified pattern.
	 * @param email to validate
	 * @return true if email is valid
	 */
	public static boolean isValidEmailInput(String email) {
		
		return email == null ? false : Pattern.matches(emailPattern, email);
		
	}
	
	/**
	 * It checks if a given date is valid or not, meaning if date is in the future.
	 * @param calendar date to check
	 * @return true if valid
	 */
	public static boolean isValidDate(Calendar calendar) {
		
		return !DateUtil.isFuture(calendar);
		
	}
	
    /**
     * It rejects validation showing a given error message into the given context.
     * It also sets the focus to the view involved.
     * @param viewToFocus view to make the focus to
     * @param errorMessage message to be shown
     */
	public static void rejectValidationForView(View viewToFocus, int errorMessage) {
    	
    	if (viewToFocus != null)
    		viewToFocus.requestFocus();
    	Toast.makeText(getAppContext(), fetchValueFor(errorMessage), 
    			Toast.LENGTH_SHORT).show();
    	
    }
	
	/**
	 * It accepts validation showing a given success message into the given context.
	 * @param successMessage message to be shown
	 */
	public static void acceptValidationForView(int successMessage) {
    	
    	Toast.makeText(getAppContext(), fetchValueFor(successMessage), 
    			Toast.LENGTH_SHORT).show();
    	
    }

}